package top.hkyzf.study.leetcode;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2024/1/22 0022 10:06
 * @version V1.0
 */
public class 最大交换_670 {
    public int maximumSwap(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int length = chars.length;
        int max = num;
        for (int i = 0; i < length-1; i++) {
            for (int j = i+1; j < length; j++) {
                int swap = swap(chars, i, j);
                max = Math.max(max, swap);
            }
        }
        return max;
    }
    private int swap(char[] chars, int i, int j) {
        // 先交换
        char iChar = chars[i];
        chars[i] = chars[j];
        chars[j] = iChar;
        int rtn = Integer.parseInt(new String(chars));
        // 再还原
        iChar = chars[i];
        chars[i] = chars[j];
        chars[j] = iChar;
        return rtn;
    }

    @Test
    public void testMaximumSwap () {
        int rtn = maximumSwap(6384);
        System.out.println(rtn);
    }
}
